package pogvue.gui;

import pogvue.analysis.AAFrequency;
import pogvue.datamodel.Alignment;
import pogvue.datamodel.AlignmentI;
import pogvue.datamodel.SequenceI;
import pogvue.io.FastaFile;

import javax.swing.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.io.IOException;


public final class FindKmers extends JFrame  {

  private final AlignmentI     align;
  private final AlignViewport  av;
  private final Controller     controller;
  private final Vector         kmers;

  private FindKmers(AlignmentI align,Vector kmers) {
 
     this.align = align;
     this.kmers = kmers;
     av         = new AlignViewport(align,false,true,true,false);
     controller = new Controller();

  }

  private Hashtable conservedKmers() {
      Hashtable conserved = new Hashtable();

      // initialize conserved to 0 for all kmers

      for (int i = 0; i < kmers.size(); i++) {
           conserved.put(((SequenceI)kmers.elementAt(i)).getSequence(), 0);
      }

      Vector sel = new Vector();

      for (int i = 0; i < align.getHeight(); i++) {
         if (align.getSequenceAt(i).getName().indexOf("Ensembl") >= 0) {
            sel.addElement(align.getSequenceAt(i));         
         }
      }
      // find all blocks in the sequence

      Hashtable blocks = AAFrequency.findBlocks(align.getSequences(),
                                                  1,
                                                  align.getWidth(),
                                                  sel);


      SequenceI ref = align.getSequenceAt(0);

      // loop over all kmers  
      for (int i = 0; i < kmers.size(); i++) {
         String kmer = ((SequenceI)kmers.elementAt(i)).getSequence();

         // foreach block
         Enumeration en = blocks.keys();

         while (en.hasMoreElements()) {
           Integer pos = (Integer)en.nextElement();
           int    len = (Integer) blocks.get(pos);

           //System.out.println("Block " + pos.intValue() + " " + len + " " + ref.getLength());
           if (len >=kmer.length() && ref.getLength() > (pos +len)) {
              int start = pos -1;
              String bstr = ref.getSequence().substring(pos -1, pos +len);

            //  System.out.println("Bstr " + pos + " " + len + " " + bstr);

              // is this kmer conserved - add one to count if so 
              if (bstr.indexOf(kmer) >= 0) {
                 int count = (Integer) conserved.get(kmer);
                 count++;
                 conserved.put(kmer, count);
                 //System.out.println("Found " + pos + " " + kmer);
              }

           }
        }
     } 
     return conserved;    
  }
  public static void main(String[] args) {

		SequenceI[] s    = null;
		SequenceI[] kmer = null;

		try {
			FastaFile ff = new FastaFile(args[0],"File");
	 		s            = ff.getSeqsAsArray();

			FastaFile kf = new FastaFile(args[1],"File");
			kmer         = kf.getSeqsAsArray();

		} catch (IOException e) {
				System.out.println("Exception reading " + args[0] + " or " + args[1]);
		}

		if (kmer != null && s != null) {
			AlignmentI align = new Alignment(kmer);

			FindKmers fk = new FindKmers(new Alignment(s),align.getSequences());

      Hashtable cons = fk.conservedKmers();

        // sort the hashtable

        // print out the conserved string 
      Enumeration en = cons.keys();

      System.out.print(args[0] + " ");

			while (en.hasMoreElements()) {
           String key = (String)en.nextElement();
           System.out.print(key + " " + cons.get(key) + " " );
        }
        System.out.println();
      }
    }
    

}
